/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * mainFrame.java
 *
 * Created on Dec 13, 2009, 2:25:32 PM
 */

package freewolf;

import com.puzzud.engine.video.VideoDisplay;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author puzzud
 */
public class MainFrame extends JFrame
{
    static MainFrame mainFrame;
    public JPanel mainPanel;
    static GamePanel gamePanel;

    /** Creates new form mainFrame */
    public MainFrame()
    {
        setIcon();

        gamePanel = new GamePanel();
        
        initComponents();
        mainPanel.add( gamePanel );

        VideoDisplay videoDisplay = gamePanel.getVideoDisplay();
        videoDisplay.setDimensions( videoDisplay.getRealWidth(), videoDisplay.getRealHeight() );

        getContentPane().setPreferredSize( new Dimension( videoDisplay.getRealWidth(), videoDisplay.getRealHeight() ) );
        pack();

        setSize( 640, 480 );

        //System.out.println( getSize().width + ":" + getSize().height );
        setLocationRelativeTo( null );
    }

    @Override
    public void setSize( int width, int height )
    {
        Dimension dimension = getContentPane().getPreferredSize();
        dimension.width = width;
        dimension.height = height;
        getContentPane().setPreferredSize( dimension );
        pack();

        VideoDisplay videoDisplay = gamePanel.getVideoDisplay();
        videoDisplay.setDimensions( width, height );
        mainPanel.setSize( width, height );
    }

    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("freewolf");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(java.awt.Color.black);
        setName("mainFrame"); // NOI18N

        mainPanel.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        mainPanel.setName("mainPanel"); // NOI18N
        //mainPanel.setPreferredSize(new java.awt.Dimension(320, 240));
        mainPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
            @Override
            public void componentResized(java.awt.event.ComponentEvent evt) {
                mainPanelComponentResized(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        getAccessibleContext().setAccessibleName("mainFrame");

        pack();
    }// </editor-fold>

    private void mainPanelComponentResized(java.awt.event.ComponentEvent evt)
    {
        Component c = evt.getComponent();

        int width = c.getWidth();
        int height = c.getHeight();

        VideoDisplay videoDisplay = gamePanel.getVideoDisplay();

        Dimension dimension = c.getPreferredSize();
        dimension.width = width;
        dimension.height = height;

        videoDisplay.setDimensions( dimension.width, dimension.height );
    }

    private void setIcon()
    {
        ImageIcon ii = new ImageIcon( this.getClass().getResource( "/graphics/freewolf_logo.png" ) );
        Image image = ii.getImage();

        this.setIconImage( image );
    }

    /**
    * @param args the command line arguments
    */
    public static void main( String args[] )
    {
        java.awt.EventQueue.invokeLater( new Runnable()
        {
            public void run()
            {
                mainFrame = new MainFrame();
                mainFrame.setVisible( true );
            }
        } );
    }
}
